package chat.config;

import com.alibaba.fastjson.JSONObject;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import java.util.List;
import java.util.Map;

public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator {

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {

        Map<String, List<String>> headers = request.getHeaders();
        // 获取用户ID和昵称
        String userId = null;
        String nickName = null;
        String userType = null; // 新增：用户类型（user/admin）
        
        if (headers.containsKey("uid") && !headers.get("uid").isEmpty()) {
            userId = headers.get("uid").get(0);
            sec.getUserProperties().put("userId", userId);
        } else {
            System.out.println("请求头中未找到用户ID(uid)");
        }
        
        if (headers.containsKey("nickName") && !headers.get("nickName").isEmpty()) {
            nickName = headers.get("nickName").get(0);
            sec.getUserProperties().put("nickName", nickName);
        } else {
            System.out.println("请求头中未找到用户nickName");
        }
        
        // 新增：获取用户类型
        if (headers.containsKey("userType") && !headers.get("userType").isEmpty()) {
            userType = headers.get("userType").get(0); // "user" 或 "admin"
            sec.getUserProperties().put("userType", userType);
        } else {
            System.out.println("请求头中未找到用户类型(userType)");
        }
    }
}
